<?php
class StoreAction extends SecurityAction {
	function _initialize() {
		$this->assign ( "menuFirst", session ( 'menuFirst' ) );
		$this->assign ( "menuSecond", session ( 'menuSecond' ) );
	}
	function slist() {
		// 超级管理员权限
		if (session ( "info.rid" ) != '1') {
			$this->redirect ( '/Admin/Console/index' );
			return;
		}
		
		// if(session('info.rid')=='1'){
		// //超级管理员
		// $sql = array();
		// }elseif(session('info.rid')=='2'){
		// //代理商
		// $sql = array('rid'=>'3','puid'=>session('info.uid'));
		// }elseif(session('info.rid')=='3'){
		// //门店
		// $sql = array('rid'=>'3','uid'=>session('info.uid'));
		// }elseif(session('info.rid')=='4'){
		// //工厂
		// $sql = array('rid'=>'4','uid'=>session('info.uid'));
		// }else{
		// $sql = array('rid'=>'0000000','uid'=>session('info.uid'));
		// }
		// $cpp = 30; //分页数
		// import('ORG.Util.Page');// 导入分页类
		// $count = $ob->where($sql)->count();// 查询满足要求的总记录数
		// $Page = new Page($count,$cpp);// 实例化分页类 传入总记录数和每页显示的记录数
		// $show = $Page->show();// 分页显示输出
		// $list = $ob->where($sql)->limit($Page->firstRow.','.$Page->listRows)->select();
		
		// $this->assign("list", $list);
		// $this->assign('show',$show);// 赋值分页输出
		
		$ob = M ( "csl_user" );
		
		//搜索名称
		if($_GET['name']!='') {
			$map [] = array (
					'name' => array (
							'like',
							"%".trim($_GET['name'])."%"
					)
			);
		}
		
		$map [] = array (
				'rid' => array (
						'neq',
						'4' 
				) 
		);
		$map [] = array (
				'status' => array (
						'neq',
						'-1' 
				) 
		);
		
		if (empty ( session ( 'info.path' ) )) {
			$map [] = array (
					'path' => array (
							'like',
							'sssssss' 
					) 
			);
		} elseif (session ( 'info.rid' ) == '1') {
		} else {
			$map [] = array (
					'path' => array (
							'like',
							session ( 'info.path' ) . '%' 
					) 
			);
		}
		
		$list = $ob->where ( $map )->limit ( 0, 2000 )->order ( array (
				'path' => 'asc' 
		) )->select ();
		foreach ( $list as $key => $value ) {
			$list [$key] ['space'] = str_repeat ( '　', (strlen ( $value ['path'] ) - 6) / 2 );
			$list [$key] ['spacenumber'] = (strlen ( $value ['path'] ) - 6) / 2;
		}
		
		$or = M ( 'csl_role' );
		$tmp = $or->select ();
		foreach ( $tmp as $key => $value ) {
			$rlist [$value ['roleid']] = $value;
		}
		
		
		$olist = $ob->where(['orderStatus'=>'0','rid'=>'7'])->select();
		$uids = [];
		foreach($olist as $value){
			$uids[] = $value['uid'];
		}
		
		$oo = M("csl_order");
		$orders = $oo->where(['uid'=>['in',$uids],'order_status'=>['EGT',7]])->select();
		foreach($olist as $key=>$value){
			foreach($orders as $key1=>$value1){
				if($value['uid']==$value1['uid']){
					$olist[$key]['orderCount'] += $value1['number'];
					unset($orders[$key1]);
				}
			}
		}
		
	//	dayin($olist);
		
		//预警代理商
		$wlist = $slist = $ilist = [];
		foreach($olist as $value){
			if(strtotime($value['ctime']) < strtotime(date('Y-m-d',time()-86400*60))
					&& $value['orderCount']<50){
				//业绩失败
						$wlist[] = $value;
			}elseif(strtotime($value['ctime']) < strtotime(date('Y-m-d',time()-86400*60))
					&& $value['orderCount']>=50){
				//业绩完成
						$slist[] = $value;
			}else{
				$ilist[] = $value;
			}
		}
		
		
		$this->assign ( 'olist', $olist ); //业绩列表
		$this->assign ( 'rlist', $rlist );
		$this->assign ( 'wlist', $wlist );
		$this->assign ( 'slist', $slist );
		$this->assign ( 'ilist', $ilist );
		$this->assign ( 'list', $list );
		$this->display ( "Store/slist" );
	}
	function detail() {
		// 超级管理员权限
		if (session ( "info.rid" ) != '1') {
			$this->redirect ( '/Admin/Console/index' );
			return;
		}
		
		
		if ($_POST) {
			// 添加
			if ($_POST ['password']) {
				$_POST ['password'] = md5 ( $_POST ['password'] . session ( 'info.salt' ) );
			} else {
				unset ( $_POST ['password'] );
			}
			
			$data = $_POST;
			
			$ob = M ( "csl_user" );
			
			if ($_POST ['uid'] != '') { // 编辑
				$data ['path'] = $_POST ['path'] . sprintf ( "%06d", $_POST ['uid'] );
				$ob->where ( array (
						'uid' => $_POST ['uid'] 
				) )->save ( $data );
			} else { // 新增
				if (trim ( $_POST ['mobileverify'] ) == '' || trim ( $_POST ['mobileverify'] ) != session ( 'mobileverify' )) { // 手机验证码错误
					session ( 'mobileverify', null );
					$this->error ( '短信验证码错误', '/Admin/Store/detail' );
				}
				
				session ( 'mobileverify', null );
				
				$info = $ob->where ( array (
						'username' => $_POST ['username'] 
				) )->find ();
				if (! empty ( $info )) {
					$this->error ( '登录账号重复，请重新添加', '/Admin/Store/detail' );
					return;
				}
				unset ( $data ['uid'] );
				$_POST ['puid'] = session ( 'info.uid' );
				$data['ctime'] = date('Y-m-d H:i:s');//账户新建的时间
				$uid = $ob->add ( $data );
				
				$path ['path'] = $_POST ['path'] . str_repeat ( '0', 6 - strlen ( $uid ) ) . $uid;
				
				$ob->where ( array (
						'uid' => $uid 
				) )->save ( $path );
			}
			
			$this->redirect ( '/Admin/Store/slist/' );
			return;
		}
		
		$or = M ( 'csl_role' );
		$addrole = $or->where ( array (
				'upid' => array (
						'like',
						'%,' . session ( 'info.rid' ) . ',%' 
				) 
		) )->select ();
		
		if ($_GET ["uid"]) {
			$ob = M ( "csl_user" );
			$info = $ob->where ( array (
					"uid" => $_GET ["uid"] 
			) )->find ();
			$info ['newpath'] = substr ( $info ['path'], 0, - 6 );
			$this->assign ( "modify", '1' );
			$this->assign ( "info", $info );
		}
		
		if (empty ( $addrole )) {
			$this->redirect ( '/Admin/Console/index' );
			return;
		}
		
		// 结构树
		$ub = D ( 'User' );
		$map = array ();
		$map [] = array (
				'rid' => array (
						'neq',
						'4' 
				) 
		);
		$map [] = array (
				'status' => array (
						'neq',
						'-1' 
				) 
		);
		if (empty ( session ( 'info.path' ) )) {
			$map [] = array (
					'path' => array (
							'like',
							'sssssss' 
					) 
			);
		} elseif (session ( 'info.rid' ) == '1') {
		} else {
			$map [] = array (
					'path' => array (
							'like',
							session ( 'info.path' ) . '%' 
					) 
			);
		}
		$childrentree = $ub->where ( $map )->order ( 'path asc' )->limit ( 0, 2000 )->getChildrenTree ();
		$this->assign ( 'childrentree', $childrentree );
		
		// dayin($childrentree);
		
		$this->assign ( 'addrole', $addrole );
		$this->display ( 'Store/detail' );
	}
	function factory() {
		$ob = M ( "csl_user" );
		$map [] = array (
				'rid' => array (
						'in',
						array (
								'4',
								'10' 
						) 
				) 
		);
		$list = $ob->where ( $map )->order ( array (
				'uid' => 'desc' 
		) )->select ();
		$this->assign ( 'list', $list );
		$this->display ( "Store/factory" );
	}
	function factorydetail() {
		if ($_POST) {
			// 添加
			if ($_POST ['password']) {
				$_POST ['password'] = md5 ( $_POST ['password'] . session ( 'info.salt' ) );
			} else {
				unset ( $_POST ['password'] );
			}
			
			$data = $_POST;
			$ob = M ( "csl_user" );
			
			if ($_POST ['uid'] != '') {
				$ob->where ( array (
						'uid' => $_POST ['uid'] 
				) )->save ( $data );
			} else {
				$info = $ob->where ( array (
						'username' => $_POST ['username'] 
				) )->find ();
				if (! empty ( $info )) {
					$this->error ( '登录账号重复，请重新添加', '/Admin/Store/factory' );
					return;
				}
				unset ( $data ['uid'] );
				$_POST ['puid'] = session ( 'info.uid' );
				$uid = $ob->add ( $data );
			}
			
			$this->redirect ( '/Admin/Store/factory/' );
			return;
		}
		
		$or = M ( 'csl_role' );
		$addrole = $or->where ( array (
				'roleid' => array (
						'in',
						array (
								'4',
								'10' 
						) 
				) 
		) )->select ();
		
		if ($_GET ["uid"]) {
			$ob = M ( "csl_user" );
			$info = $ob->where ( array (
					"uid" => $_GET ["uid"] 
			) )->find ();
			$this->assign ( "modify", '1' );
			$this->assign ( "info", $info );
		}
		
		$this->assign ( 'addrole', $addrole );
		$this->display ( "Store/factorydetail" );
	}
	
	// 修改库存数量
	function inventory() {
		$ob = M ( "csl_user" );
		$map = array (
				"uid" => $_GET ["uid"] 
		);
		$info = $ob->where ( $map )->find ();
		
		if (session ( "info.rid" ) != '1') {
			$this->redirect ( '/Admin/Store/slist' );
			return;
		}
		
		if ($_POST) {
			
			$newInventory = intval ( $_POST ['inventory'] );
			$msg = "修改 {$info[name]}[ {$info['username']} ] 印刷码数量 ： [ {$info[inventory]} ] => [ {$newInventory} ]";
			
			if ($newInventory == $info ['inventory']) {
				$this->redirect ( '/Admin/Store/inventory/uid/' . $_GET ["uid"] . '?r=' . time () );
				return;
			}
			
			$return = $ob->where ( array (
					$map 
			) )->setField ( 'inventory', $newInventory );
			if ($return) {
				LogTransactionModel::ModifyInventory ( $info ['uid'], '', '修改印刷码 [成功]', $msg );
			} else {
				LogTransactionModel::ModifyInventory ( $info ['uid'], '', '修改印刷码 [失败]', $msg );
			}
			
			$this->redirect ( '/Admin/Store/inventory/uid/' . $_GET ["uid"] . '?r=' . time () );
			return;
		}
		
		$this->assign ( "info", $info );
		
		$ol = M ( "log_transaction" );
		$log = $ol->where ( array (
				'uid' => $_GET ['uid'] 
		) )->order ( array (
				'id' => 'desc' 
		) )->limit ( 0, 2000 )->select ();
		$this->assign ( "log", $log );
		
		$this->display ( 'Store/inventory' );
	}
	
	// function saveData(){
	
	// }
	
	// 修改当前登录人密码
	public function updatePass() {
		// 修改密码
		if ($_POST) {
			$ob = M ( "csl_user" );
			
			$_POST ["password"] = md5 ( $_POST ["password"] . session ( 'info.salt' ) );
			
			$ob->where ( array (
					"uid" => $_POST ["uid"] 
			) )->save ( $_POST );
			$this->redirect ( "/Admin/Store/slist" );
			return;
		}
		
		// 修改菜单跳转修改页面
		$info ["uid"] = session ( 'info.uid' );
		$info ["username"] = session ( 'info.username' );
		$this->assign ( "info", $info );
		$this->display ( "/Store/personInfo" );
	}
	
	
	//核心代理商管理
	function slist2() {
		//官方代理商权限
		if (session ( "info.rid" ) != '5') {
			$this->redirect ( '/Admin/Console/index' );
			return;
		}
			
		$ob = M ( "csl_user" );
		
		
		//搜索名称
		if($_GET['name']!='') {
			$map [] = array (
					'name' => array (
							'like',
							"%".trim($_GET['name'])."%"
					)
			);
		}
		
		$map [] = array (
				'puid' => array (
						'eq',
						session('info.uid')
				)
		);
		
		$map [] = array (
				'path' => array (
						'like',
						session ( 'info.path' ) . '%'
				)
		);
		
		$map [] = array (
				'status' => array (
						'neq',
						'-1'
				)
		);
		
	
		$list = $ob->where ( $map )->order ( array (
				'path' => 'asc'
		) )->limit ( 0, 2000 )->select ();
		foreach ( $list as $key => $value ) {
			$list [$key] ['space'] = str_repeat ( '　', (strlen ( $value ['path'] ) - 6) / 2 );
			$list [$key] ['spacenumber'] = (strlen ( $value ['path'] ) - 6) / 2;
		}
		
		$or = M ( 'csl_role' );
		$tmp = $or->select ();
		foreach ( $tmp as $key => $value ) {
			$rlist [$value ['roleid']] = $value;
		}
		
		$this->assign ( 'rlist', $rlist );
		$this->assign ( 'list', $list );
		$this->display ( "Store/slist2" );
	}
	
	//核心代理商管理--详情编辑
	function detail2() {
		//官方代理商权限
		if (session ( "info.rid" ) != '5') {
			$this->redirect ( '/Admin/Console/index' );
			return;
		}
	
		if ($_POST) {
			// 添加
			if ($_POST ['password']) {
				$_POST ['password'] = md5 ( $_POST ['password'] . session ( 'info.salt' ) );
			} else {
				unset ( $_POST ['password'] );
			}
				
			$data = $_POST;
				
			$ob = M ( "csl_user" );
				
			if ($_POST ['uid'] != '') { // 编辑
				$data ['path'] = $_POST ['path'] . sprintf ( "%06d", $_POST ['uid'] );
				$ob->where ( array (
						'uid' => $_POST ['uid']
				) )->save ( $data );
			} else { // 新增
				if (trim ( $_POST ['mobileverify'] ) == '' || trim ( $_POST ['mobileverify'] ) != session ( 'mobileverify' )) { // 手机验证码错误
					session ( 'mobileverify', null );
					$this->error ( '短信验证码错误', '/Admin/Store/detail2' );
				}
	
				session ( 'mobileverify', null );
	
				$info = $ob->where ( array (
						'username' => $_POST ['username']
				) )->find ();
				if (! empty ( $info )) {
					$this->error ( '登录账号重复，请重新添加', '/Admin/Store/detail2' );
					return;
				}
				unset ( $data ['uid'] );
				$_POST ['puid'] = session ( 'info.uid' );
				$data['ctime'] = date('Y-m-d H:i:s');//账户新建的时间
				$uid = $ob->add ( $data );
	
				$path ['path'] = $_POST ['path'] . str_repeat ( '0', 6 - strlen ( $uid ) ) . $uid;
	
				$ob->where ( array (
						'uid' => $uid
				) )->save ( $path );
			}
				
			$this->redirect ( '/Admin/Store/slist2/' );
			return;
		}
	
		/*
		$or = M ( 'csl_role' );
		$addrole = $or->where ( array (
				'upid' => array (
						'like',
						'%,' . session ( 'info.rid' ) . ',%'
				)
		) )->select ();
		if (empty ( $addrole )) {
			$this->redirect ( '/Admin/Console/index' );
			return;
		}
		*/
		
	
		if ($_GET ["uid"]) {
			$ob = M ( "csl_user" );
			$info = $ob->where ( array (
					"uid" => $_GET ["uid"]
			) )->find ();
			$info ['newpath'] = substr ( $info ['path'], 0, - 6 );
			$this->assign ( "modify", '1' );
			$this->assign ( "info", $info );
		}
	
		
	
		// 结构树
		$ub = D ( 'User' );
		$map = array ();
		$map [] = array (
				'path' => array (
						'like',
						session ( 'info.path' ) . '%'
				)
		);
		$childrentree = $ub->where ( $map )->order ( 'path asc' )->limit ( 0, 2000 )->getChildrenTree ();
		$this->assign ( 'childrentree', $childrentree );
	//	dayin($ub->getlastsql());
	//	dayin($childrentree);
	
		$this->assign ( 'addrole', $addrole );
		$this->display ( 'Store/detail2' );
	}
}

?>